From 48399a1b76a148ede2840da1659f923eb733079a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 11 Sep 2014 08:09:47 -0700 Subject: [PATCH] Pass profile env vars to build commands Closes #553 --- src/cargo/ops/cargo_rustc/context.rs | 2 +- src/cargo/ops/cargo_rustc/mod.rs | 14 +++++++++++++- src/doc/native-build.md | 8 ++++++++ tests/test_cargo_compile.rs | 14 ++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index bb5e88301..2cb50d397 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -22,8 +22,8 @@ pub struct Context<'a, 'b> { pub resolve: &'a Resolve, pub sources: &'a SourceMap<'b>, pub compilation: Compilation, + pub env: &'a str, - env: &'a str, host: Layout, target: Option, target_triple: String, diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index d53736aa4..ab155d31f 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -170,6 +170,15 @@ fn compile<'a, 'b>(targets: &[&'a Target], pkg: &'a Package, fn compile_custom(pkg: &Package, cmd: &str, cx: &Context, first: bool) -> CargoResult { + let root = cx.get_package(cx.resolve.root()); + let profile = root.get_manifest().get_targets().iter() + .find(|target| target.get_profile().get_env() == cx.env) + .map(|target| target.get_profile()); + let profile = match profile { + Some(profile) => profile, + None => return Err(internal(format!("no profile for {}", cx.env))) + }; + // TODO: this needs to be smarter about splitting let mut cmd = cmd.split(' '); // TODO: this shouldn't explicitly pass `KindTarget` for dest/deps_dir, we @@ -180,7 +189,10 @@ fn compile_custom(pkg: &Package, cmd: &str, let mut p = process(cmd.next().unwrap(), pkg, cx) .env("OUT_DIR", Some(&output)) .env("DEPS_DIR", Some(&output)) - .env("TARGET", Some(cx.target_triple())); + .env("TARGET", Some(cx.target_triple())) + .env("DEBUG", Some(profile.get_debug().to_string())) + .env("OPT_LEVEL", Some(profile.get_opt_level().to_string())) + .env("PROFILE", Some(profile.get_env())); for arg in cmd { p = p.arg(arg); } diff --git a/src/doc/native-build.md b/src/doc/native-build.md index 6fa5d9740..60a169487 100644 --- a/src/doc/native-build.md +++ b/src/doc/native-build.md @@ -78,6 +78,14 @@ commands. directory in which all the output of the dependency's build command was placed. This is useful for picking up things like header files and such from other packages. +* `CARGO_MANIFEST_DIR` - The directory containing the manifest for the package + being built. +* `OPT_LEVEL`, `DEBUG` - values of the corresponding variables for the + profile currently being built. +* `PROFILE` - name of the profile currently being built (see + [profiles][profile]). + +[profile]: manifest.html#the-[profile.*]-sections # A complete example diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index 5809dc331..03b3c3dfd 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -793,6 +793,15 @@ test!(custom_build_env_vars { use std::io::fs::PathExtensions; fn main() {{ let _ncpus = os::getenv("NUM_JOBS").unwrap(); + let debug = os::getenv("DEBUG").unwrap(); + assert_eq!(debug.as_slice(), "true"); + + let opt = os::getenv("OPT_LEVEL").unwrap(); + assert_eq!(opt.as_slice(), "0"); + + let opt = os::getenv("PROFILE").unwrap(); + assert_eq!(opt.as_slice(), "compile"); + let out = os::getenv("OUT_DIR").unwrap(); assert!(out.as_slice().starts_with(r"{0}")); assert!(Path::new(out).is_dir()); @@ -800,6 +809,11 @@ test!(custom_build_env_vars { let out = os::getenv("DEP_BAR_BAR_OUT_DIR").unwrap(); assert!(out.as_slice().starts_with(r"{0}")); assert!(Path::new(out).is_dir()); + + let out = os::getenv("CARGO_MANIFEST_DIR").unwrap(); + let p1 = Path::new(out); + let p2 = os::make_absolute(&Path::new(file!()).dir_path().dir_path()); + assert!(p1 == p2, "{{}} != {{}}", p1.display(), p2.display()); }} "#, p.root().join("target").join("native").display())); -- 2.30.2